// gui/GuiTickBox.cpp

#include "GuiTickBox.h"
#include <gfx/Texture.h>

TypeId GUI::GuiTickBox::s_TypeId = "GuiTickBox";

using namespace GUI;

void GuiTickBox::SetupGuiTickBox(GFX::Texture * pBoxTexture,
								GFX::Texture * pTickTexture)
{
	Math::vec3 vSpritePos(0.0f, 0.0f, 0.0f);
	CORE::Colour colColour(255, 255, 255);
	m_BoxSprite.SetupGuiComponent(GetName() + "_BACKGROUND");
	m_BoxSprite.SetupGuiSprite(vSpritePos, pBoxTexture, colColour);

	m_TickSprite.SetupGuiComponent(GetName() + "_TICKS");
	m_TickSprite.SetupGuiSprite(vSpritePos, pTickTexture, colColour);

	GetFrame().AttachFrame(&m_BoxSprite.GetFrame());
	GetFrame().AttachFrame(&m_TickSprite.GetFrame());

	float32_t fHalfWidth = static_cast<float32_t>(pBoxTexture->GetWidth()) / 2.0f;
	float32_t fHalfHeight = static_cast<float32_t>(pTickTexture->GetHeight()) / 2.0f;

	m_HotSpotRect.SetRect(-fHalfWidth, fHalfHeight, fHalfWidth, -fHalfHeight);
	m_HotSpotRect.SetFrame(&GetFrame());
	SetHotSpot(&m_HotSpotRect);

	SetVisible(true);

	m_bVisualDirty = true;
}

void GuiTickBox::Update(CORE::Time & TimeDelta)
{
	GuiSelectable::Update(TimeDelta);

	m_BoxSprite.Update(TimeDelta);
	m_TickSprite.Update(TimeDelta);
}

void GuiTickBox::Render(IRenderingEngine * pRenderer)
{
	if (IsVisible())
	{
		if (m_bVisualDirty)
		{
			GFX::VisualSprite * pBoxSprite = static_cast<GFX::VisualSprite *>(m_BoxSprite.GetVisual());
			GFX::VisualSprite * pTickSprite = static_cast<GFX::VisualSprite *>(m_TickSprite.GetVisual());

			CORE::Colour colColour(255, 255, 255);

			if (IsHighlightable())
			{
				if (IsActive())
					colColour.SetColour(255, 255, 255);
				else
					colColour.SetColour(128, 128, 128);
			}
			else
				colColour.SetColour(64, 64, 64);

			pBoxSprite->SetColour(colColour);
			pTickSprite->SetColour(colColour);

			m_bVisualDirty = false;
		}

		m_BoxSprite.Render(pRenderer);
		if (m_bTicked)
			m_TickSprite.Render(pRenderer);

		GuiSelectable::Render(pRenderer);
	}
}